home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / gcc / ixemlsrc.lha / ixemul / ixnet / getpwent.c < prev    next >
C/C++ Source or Header  |  1996-03-13  |  7KB  |  272 lines

  1. /*
  2.  *  This file is part of ixnet.library for the Amiga.
  3.  *  Copyright (C) 1996 Jeff Shepherd
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  $Id:$
  20.  *
  21.  *  $Log:$
  22.  *
  23.  */
  24.  
  25. #define KERNEL
  26. #include <pwd.h>
  27. #include "ixnet.h"
  28.  
  29. #include <fcntl.h>
  30. #include <db.h>
  31. #include <utmp.h>
  32. #include <errno.h>
  33. #include <unistd.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <limits.h>
  37. #include <time.h>
  38.  
  39. static struct passwd _pw_passwd;    /* password structure */
  40. static DB *_pw_db;            /* password database */
  41. static int _pw_keynum;            /* key counter */
  42. static int _pw_stayopen;        /* keep fd's open */
  43. static int __hashpw(), __initdb();
  44.  
  45. /* prototypes */
  46. static struct passwd *__TCP2InetPwd(struct TCP_passwd *pwd);
  47.  
  48. struct passwd *
  49. getpwent(void) {
  50.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  51.     register int network_protocol = p->u_networkprotocol;
  52.  
  53.     if (network_protocol == IX_NETWORK_AMITCP) {
  54.     struct TCP_passwd *err = UG_getpwent(p->u_UserGroupBase);
  55.  
  56.     if (err == NULL) {
  57.         errno = ug_GetErr(p->u_UserGroupBase);
  58.         return NULL;
  59.     }
  60.     else {
  61.         memcpy(&_pw_passwd,__TCP2InetPwd(err),sizeof(struct passwd));
  62.         return &_pw_passwd;
  63.     }
  64.     }
  65.     else /*if (network_protocol == IX_NETWORK_AS225)*/ {
  66.     DBT key;
  67.     char bf[sizeof(_pw_keynum) + 1];
  68.  
  69.     if (!_pw_db && !__initdb())
  70.         return((struct passwd *)NULL);
  71.  
  72.     ++_pw_keynum;
  73.     bf[0] = _PW_KEYBYNUM;
  74.     bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
  75.     key.data = (u_char *)bf;
  76.     key.size = sizeof(_pw_keynum) + 1;
  77.     return(__hashpw(&key) ? &_pw_passwd : (struct passwd *)NULL);
  78.     }
  79. }
  80.  
  81. struct passwd *
  82. getpwnam(const char *name) {
  83.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  84.     register int network_protocol = p->u_networkprotocol;
  85.  
  86.     if (network_protocol == IX_NETWORK_AMITCP) {
  87.     struct TCP_passwd *err = UG_getpwnam(p->u_UserGroupBase,name);
  88.  
  89.     if (err == NULL) {
  90.         errno = ug_GetErr(p->u_UserGroupBase);
  91.         return NULL;
  92.     }
  93.     else {
  94.         memcpy(&_pw_passwd,__TCP2InetPwd(err),sizeof(struct passwd));
  95.     }
  96.     return &_pw_passwd;
  97.     }
  98.     else /* if (network_protocol == IX_NETWORK_AS225)*/ {
  99.     DBT key;
  100.     int len, rval;
  101.     char bf[UT_NAMESIZE + 1];
  102.  
  103.     if (!_pw_db && !__initdb())
  104.         return((struct passwd *)NULL);
  105.  
  106.     bf[0] = _PW_KEYBYNAME;
  107.     len = strlen(name);
  108.     bcopy(name, bf + 1, MIN(len, UT_NAMESIZE));
  109.     key.data = (u_char *)bf;
  110.     key.size = len + 1;
  111.     rval = __hashpw(&key);
  112.  
  113.     if (!_pw_stayopen) {
  114.         (void)(_pw_db->close)(_pw_db);
  115.         _pw_db = (DB *)NULL;
  116.     }
  117.     return(rval ? &_pw_passwd : (struct passwd *)NULL);
  118.     }
  119. }
  120.  
  121. struct passwd *
  122. getpwuid(uid_t uid) {
  123.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  124.     register int network_protocol = p->u_networkprotocol;
  125.  
  126.     /* Don't do this if uid == -2 (nobody2) */
  127.     /* This happens when someone doesn't use AmiTCP's login */
  128.     if (network_protocol == IX_NETWORK_AMITCP) {
  129.     if (uid != (uid_t)-2) {
  130.         struct TCP_passwd *err = UG_getpwuid(p->u_UserGroupBase,uid);
  131.  
  132.         if (err == NULL) {
  133.         errno = ug_GetErr(p->u_UserGroupBase);
  134.         return NULL;
  135.         }
  136.         else {
  137.         memcpy(&_pw_passwd,__TCP2InetPwd(err),sizeof(struct passwd));
  138.         }
  139.         return &_pw_passwd;
  140.     }
  141.     else {
  142.         return getpwnam(getenv("USER"));
  143.     }
  144.     }
  145.     else /*if (network_protocol == IX_NETWORK_AS225)*/  {
  146.     DBT key;
  147.     int keyuid, rval;
  148.     char bf[sizeof(keyuid) + 1];
  149.  
  150.     if (!_pw_db && !__initdb())
  151.         return((struct passwd *)NULL);
  152.  
  153.     bf[0] = _PW_KEYBYUID;
  154.     keyuid = uid;
  155.     bcopy(&keyuid, bf + 1, sizeof(keyuid));
  156.     key.data = (u_char *)bf;
  157.     key.size = sizeof(keyuid) + 1;
  158.     rval = __hashpw(&key);
  159.  
  160.     if (!_pw_stayopen) {
  161.         (void)(_pw_db->close)(_pw_db);
  162.         _pw_db = (DB *)NULL;
  163.     }
  164.     return(rval ? &_pw_passwd : (struct passwd *)NULL);
  165.     }
  166. }
  167.  
  168. int
  169. setpassent(int stayopen) {
  170.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  171.  
  172.     if (p->u_networkprotocol == IX_NETWORK_AS225) {
  173.     _pw_keynum = 0;
  174.     _pw_stayopen = stayopen;
  175.     }
  176.     return(1);
  177. }
  178.  
  179. int
  180. setpwent(void) {
  181.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  182.     register int network_protocol = p->u_networkprotocol;
  183.  
  184.     if (network_protocol == IX_NETWORK_AMITCP) {
  185.     return UG_setpwent(p->u_UserGroupBase);
  186.     }
  187.     else /* if (network_protocol == IX_NETWORK_AS225) */ {
  188.     _pw_keynum = 0;
  189.     _pw_stayopen = 0;
  190.     }
  191.     return 1;
  192. }
  193.  
  194. void
  195. endpwent(void) {
  196.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  197.     register int network_protocol = p->u_networkprotocol;
  198.  
  199.     if (network_protocol == IX_NETWORK_AMITCP) {
  200.     UG_endpwent(p->u_UserGroupBase);
  201.     }
  202.     else /* if (network_protocol == IX_NETWORK_AS225)*/ {
  203.     _pw_keynum = 0;
  204.     if (_pw_db) {
  205.         (void)(_pw_db->close)(_pw_db);
  206.         _pw_db = (DB *)NULL;
  207.     }
  208.     }
  209. }
  210.  
  211. static int
  212. __initdb(void) {
  213.     char *p;
  214.  
  215.     p = (geteuid()) ? _PATH_MP_DB : _PATH_SMP_DB;
  216.     _pw_db = hash_open(p, O_RDONLY, 0, NULL);
  217.     if (_pw_db)
  218.     return(1);
  219.     return(0);
  220. }
  221.  
  222. static      int
  223. __hashpw(DBT *key) {
  224.     register char *p, *t;
  225.     static u_int max;
  226.     static char *line;
  227.     DBT data;
  228.  
  229.     if ((_pw_db->get)(_pw_db, key, &data, 0))
  230.     return(0);
  231.  
  232.     p = (char *)data.data;
  233.     if (data.size > max && !(line = realloc(line, max += 1024)))
  234.     return(0);
  235.  
  236.     t = line;
  237. #define EXPAND(e)       e = t; while ((*t++ = *p++));
  238.     EXPAND(_pw_passwd.pw_name);
  239.     EXPAND(_pw_passwd.pw_passwd);
  240.     bcopy(p, (char *)&_pw_passwd.pw_uid, sizeof(int));
  241.     p += sizeof(int);
  242.     bcopy(p, (char *)&_pw_passwd.pw_gid, sizeof(int));
  243.     p += sizeof(int);
  244.     bcopy(p, (char *)&_pw_passwd.pw_change, sizeof(time_t));
  245.     p += sizeof(time_t);
  246.     EXPAND(_pw_passwd.pw_class);
  247.     EXPAND(_pw_passwd.pw_gecos);
  248.     EXPAND(_pw_passwd.pw_dir);
  249.     EXPAND(_pw_passwd.pw_shell);
  250.     bcopy(p, (char *)&_pw_passwd.pw_expire, sizeof(time_t));
  251.     p += sizeof(time_t);
  252.     return(1);
  253. }
  254.  
  255. /* change the AmiTCP password structure to the global format */
  256. static struct passwd *__TCP2InetPwd(struct TCP_passwd *pwd) {
  257.     static struct passwd retval;
  258.     retval.pw_name = pwd->pw_name;
  259.     retval.pw_passwd = pwd->pw_passwd;
  260.     retval.pw_uid = pwd->pw_uid;
  261.     retval.pw_gid = pwd->pw_gid;
  262.     retval.pw_change = time((time_t *)NULL);
  263.     retval.pw_class = NULL;
  264.     retval.pw_gecos = pwd->pw_gecos;
  265.     retval.pw_dir = pwd->pw_dir;
  266.     retval.pw_shell = pwd->pw_shell;
  267.     retval.pw_expire = (time_t)-1;
  268.     return &retval;
  269. }
  270.  
  271.  
  272.